package networkinstance;

/**
 * 1、建立服务器端
 * 服务器建立通信ServerSocket
 * 服务器建立Socket接收客户端连接
 * 建立IO输入流读取客户端发送的数据
 * 建立IO输出流向客户端发送数据消息
 */
public class Server {
    public static void main(String[] args) {
        try {
            java.net.ServerSocket ss = new java.net.ServerSocket(8888);
            System.out.println("启动服务器....");
            java.net.Socket s = ss.accept();
            System.out.println("客户端:" + s.getInetAddress().getLocalHost() + "已连接到服务器");

            java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream()));
            //读取客户端发送来的消息
            String mess = br.readLine();
            System.out.println("客户端：" + mess);
            java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.OutputStreamWriter(s.getOutputStream()));
            bw.write(mess + "\n");
            bw.flush();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}
